package com.shop.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.shop.admin.dao.TakeoutMerchantCategoryDao;
import com.shop.admin.dao.TakeoutMerchantCategoryRelationDao;
import com.shop.admin.dao.TakeoutMerchantDao;
import com.shop.admin.entity.ShopCategory;
import com.shop.admin.entity.TakeoutMerchant;
import com.shop.admin.entity.TakeoutMerchantCategory;
import com.shop.admin.service.TakeoutMerchantCategoryService;
import com.shop.admin.utils.PageUtils;
import com.shop.admin.utils.Query;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Service
public class TakeoutMerchantCategoryServiceImpl extends ServiceImpl<TakeoutMerchantCategoryDao, TakeoutMerchantCategory> implements TakeoutMerchantCategoryService {

    @Resource
    private TakeoutMerchantCategoryRelationDao takeoutMerchantCategoryRelationDao;

    @Resource
    private TakeoutMerchantDao takeoutMerchantDao;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<TakeoutMerchantCategory> page = this.page(
                new Query<TakeoutMerchantCategory>().getPage(params),
                new QueryWrapper<TakeoutMerchantCategory>()
        );
        return new PageUtils(page);
    }

    @Override
    public boolean selectBoolean(String cateName) {
        if (this.baseMapper.selectOne(new QueryWrapper<TakeoutMerchantCategory>().lambda().eq(TakeoutMerchantCategory::getCateName,cateName))!=null){
            return false;
        }
        return true;
    }

    @Override
    public void updateCascade(TakeoutMerchantCategory category) {
        category.setUpdateTime(new Date());
        this.updateById(category);
    }

    @Override
    @Transactional
    public void removeCategoryByIds(List<Long> asList) {
        //删除分类表数据
        this.baseMapper.deleteBatchIds(asList);
        //删除关系表数据
        takeoutMerchantCategoryRelationDao.deleteBatchIds(asList);
        //根据关系表删除商家信息（逻辑删除）
        TakeoutMerchant takeoutMerchant = TakeoutMerchant.builder().id(asList.get(0).intValue()).beenDeleted(0).deleteTime(new Date()).build();
        takeoutMerchantDao.updateById(takeoutMerchant);
    }
}
